#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavBarButton.h>
#import <UIKit/UINavigationItem.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIImage.h>
#import <UIKit/UIButtonBar.h>
#import <UIKit/UITextView.h>
#import <UIKit/UIImageView.h>
#import <UIKit/UIKeyboard.h>
#include <unistd.h>
#import "DrillView.h"
#import "CardView.h"

@implementation CardTransitionView : UITransitionView



@end

@implementation DrillView : UIView

- (id) initWithApplication: (UIApplication*)app withAppID: (NSString*)appID withFrame: (struct CGRect)rect
{
	//Init view with frame rect
	[super initWithFrame: rect];
	
	//Save application object for launching other apps
	_app = app;
	_appID = appID;
	
	// Set up the cards
	// Control sizes
	float statusBarWidth = rect.size.width;
	float statusBarHeight = 46.0f;
	float navBarWidth = rect.size.width;
	float navBarHeight = 42.0f;
	float navButtonTopOffset = 39.0f;
	float answerButtonWidth = 80.0f;
	float nextButtonWidth = 65.0f;
	float nextButtonHeight = 32.0f;
	float sortButtonWidth = 60.0f;
	_textHeightAvail = rect.size.height - navBarHeight - statusBarHeight;
	_cardRect = CGRectMake(0.0f, 42.0f, 320.0f, _textHeightAvail);
	
	// Set up the "Card Area"
	_cardTransition = [[CardTransitionView alloc] initWithFrame: _cardRect ];
	
	
	// Setup Navigation bar
	UINavigationBar* _navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, navBarWidth, navBarHeight)];
	UINavigationItem* title = [[UINavigationItem alloc] initWithTitle:@"iStudy"];
	[_navBar pushNavigationItem:title];
	[_navBar setBarStyle: 0];
	[_navBar enableAnimation];
	
	UINavBarButton* _listButton = [[UINavBarButton alloc] initWithFrame: CGRectMake(3.0f, 8.0f, nextButtonWidth, nextButtonHeight)];
	[_listButton setAutosizesToFit: NO];
	[_listButton setTitle: @"	 Cards"];
	[_listButton addTarget: self action: @selector(makeSetBrowserActive) forEvents: 1];
	[_listButton setNavBarButtonStyle: 2];
	[_navBar addSubview: _listButton];
	
	// Setup Buttons
	/*
		Button Styles
		0 - Dark Blue Rectangle
		1 - Dark Red Rectangle
		2 - Dark Blue Left Arrow
		3 - Light Blue Rectangle
	*/
	
	// Setup Status Bar
	_statusBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, rect.size.height - statusBarHeight, statusBarWidth, statusBarHeight)];
	[_statusBar setPrompt: @"Status Bar"];
	
	UINavBarButton* shuffle = [[UINavBarButton alloc] initWithFrame: CGRectMake(3.0f, 8.0f, sortButtonWidth, nextButtonHeight)];
	[shuffle setAutosizesToFit: NO];
	[shuffle setTitle: @"Shuffle"];
	[shuffle addTarget: self action: @selector(shuffleCards) forEvents: 1];
	[shuffle setNavBarButtonStyle: 0];
	[_statusBar addSubview: shuffle];
	
	[self addSubview:_navBar];
	[self addSubview:_cardTransition];
	[self addSubview:_statusBar];
	
	return self;

}

/**
 * Shuffles the Flash Cards
**/
- (void) shuffleCards
{
	int total = [_cards count];
	int one;
	int other;
	
	int i;
	for (i=0;i<total;i++)
	{
		one = (rand()%total);
		other = (rand()%total);
		if (one != other) [_cards exchangeObjectAtIndex:one withObjectAtIndex:other];
	}
	
	_cardPosition = -1;
	[self nextQuestion];
}


/**
 * Setup drill with new card set
**/
- (void) setupDrill: (NSString*)pathToSet
{
	
	// question.txt full path
	NSString* question_path = [[NSString alloc] initWithFormat: @"%@/questions.txt", pathToSet];
	// answers.txt full path
	NSString* answer_path = [[NSString alloc] initWithFormat: @"%@/answers.txt", pathToSet];
	
	// Read in the questions and answers
	NSString* qfile = [[NSString alloc] initWithContentsOfFile: question_path];
	NSString* afile = [[NSString alloc] initWithContentsOfFile: answer_path];
	NSArray* questions = [qfile componentsSeparatedByString: @"\n"]; 
	NSArray* answers = [afile componentsSeparatedByString: @"\n"]; 
	
	_totalCards = [questions count];
	
	// Load the array of cards
	[_cards dealloc];
	_cards = [[NSMutableArray alloc] initWithCapacity:_totalCards];
	
	NSLog(@"Loading cards with Q/A pairs...");
	
	CardView* card;
	
	int i;
	for(i=0;i<_totalCards;i++)
	{
		card = [[CardView alloc] initWithDrillView:self withFrame:_cardRect withHeight:_textHeightAvail];
		[card setQuestion:[questions objectAtIndex:i] andAnswer:[answers objectAtIndex:i]];
		[_cards addObject:card];
	}
	
	// set up card position in arrays
	_cardPosition = -1;
	
	// load up first question
	[self nextQuestion];
	
	NSLog(@"Setup flash cards at %@",pathToSet);
}

/**
 * Loads the set browser
**/
- (void) makeSetBrowserActive
{
	[_app makeSetBrowserActive];
}

/**
 * Update Status Bar
**/
- (void) updateStatusBar
{
	
	// Update the Status Bar
	NSString* new_prompt = [[NSString alloc] initWithFormat: @"Question %i of %i.", _cardPosition+1, _totalCards];
	[_statusBar setPrompt: new_prompt];
	
}

/**
 * Loads up the next question
**/
- (void) nextQuestion
{
	if (_cardPosition != -1)
	{
		// reset the previous card
		[[_cards objectAtIndex:_cardPosition] resetCard];
	}
	
	_cardPosition = _cardPosition + 1;
	if (_cardPosition >= _totalCards) _cardPosition = 0;
	
	// transition to card
	[_cardTransition transition:1 toView:[_cards objectAtIndex:_cardPosition]];
	
	
	[self updateStatusBar];
}

/**
 * Loads up the previous question
**/
- (void) backQuestion
{
	// reset the previous card
	[[_cards objectAtIndex:_cardPosition] resetCard];
	
	_cardPosition = _cardPosition - 1;
	if (_cardPosition < 0) _cardPosition = _totalCards - 1;
	
	// transition to card
	[_cardTransition transition:2 toView:[_cards objectAtIndex:_cardPosition]];
	
	[self updateStatusBar];
}


/**
 * Delegate Methods
**/



@end